<?php

namespace app\admin\controller;
use app\admin\controller\Main;
use think\Controller;
use think\Db;

class Agent extends Main {

    public function index() {

        $_GPC = input();
        $page = max(intval($_GPC['page']), 1);
        $limit = max(intval($_GPC['limit']), 1);
        $row = Db::name('_member')->where('isagent', 1);
        if (!empty($_GPC['id'])) {
            $row = $row->where('id', $_GPC['id']);
        }
        if (!empty($_GPC['nickname'])) {
            $row = $row->where('nickname LIKE', "%{$_GPC['nickname']}%");
        }
        if (!empty($_GPC['openid'])) {
            $row = $row->where('openid LIKE', "%{$_GPC['openid']}%");
        }
        if (isset($_GPC['gender']) && $_GPC['gender'] != '') {
            $row = $row->where('gender', $_GPC['gender']);
        }
        if (!empty($_GPC['mobile'])) {
            $row = $row->where('mobile LIKE', "%{$_GPC['mobile']}%");
        }
        $count = $row->count();
        $data = $row->page($page, $limit)->order('createtime', 'DESC')->select();
        jsonpp($data, $count);
    }

    public function add() {

        $_GPC = input();
        if ($this->request->isPost()) {
            $token = check_formtoken();
             unset($_GPC['data']['token']);
            $_GPC['data']['isagent'] = 1;
            $_GPC['data']['createtime'] = time();
            $_GPC['data']['endtime'] = strtotime($_GPC['data']['endtime']);
            list($msec, $sec) = explode(' ', microtime());
            $msectime = (float) sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
            $_GPC['data']['salt'] = $msectime;
            if (!isset($_GPC['data']['pwd']) || $_GPC['data']['pwd'] == '') {
                $_GPC['data']['pwd'] = md5('123456' . $msectime);
            } elseif((isset($_GPC['data']['pwd']) || $_GPC['data']['pwd'] != '')) {
                $_GPC['data']['pwd'] = md5($_GPC['data']['pwd'] . $msectime);
            }
            $result = Db::name('member')->where(array('openid' => $_GPC['data']['openid']))->find();
            if (!empty($result) || $result === 0) {
                if ($result['isagent'] == '1') {
                    json(array('token' => $token), 1, '代理已存在！');
                }
                if ($_GPC['data']['avatar'] == '/upload/avatar.png') {
                    unset($_GPC['data']['avatar']);
                }
                if ($_GPC['data']['nickname'] == '') {
                    unset($_GPC['data']['nickname']);
                }
                if ($result['agent_id'] == 0) {
                    $_GPC['data']['agent_id'] = $result['id'];
                }
                $result = Db::name('member')->where(array('id' => $result['id']))->update($_GPC['data']);
            } else {
                unset($_GPC['data']['token']);
                $id = Db::name('member')->insertGetId($_GPC['data']);
                $result = Db::name('member')->where(array('id' => $id))->update(array('agent_id' => $id));
            }
            if (!empty($result) || $result === 0) {
                json(array('token' => $token));
            } else {
                json(array('token' => $token), 1, '提交失败！');
            }
        }
        $data = array('avatar' => '/upload/avatar.png');
        $data['level'] = Db::name('agent_level')->order('type', 'DESC')->order('level', 'DESC')->select();
        jsont(get_upload_data($data));
    }

    public function edit() {

        $_GPC = input();
        $id = $_GPC['id'];
        if ($this->request->isPost()) {
            $token = check_formtoken();
             unset($_GPC['data']['token']);
            $_GPC['data']['isagent'] = 1;
            $_GPC['data']['endtime'] = strtotime($_GPC['data']['endtime']);
            if ($_GPC['data']['pwd'] == '') {
                unset($_GPC['data']['pwd']);
            } else {
                list($msec, $sec) = explode(' ', microtime());
                $msectime = (float) sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);
                $_GPC['data']['pwd'] = md5($_GPC['data']['pwd'] . $msectime);
                $_GPC['data']['salt'] = $msectime;
            }
            $result = Db::name('member')->where(array('id' => $id))->update($_GPC['data']);
            if (!empty($result) || $result === 0) {
                json(array('token' => $token));
            } else {
                json(array('token' => $token), 1, '提交失败！');
            }
        }
        $data = Db::name('member')->where(array('id' => $id))->find();
        $data['endtime'] = date('Y-m-d', $data['endtime']);
        $data['level'] = Db::name('agent_level')->order('type', 'DESC')->order('level', 'DESC')->select();
        jsont(get_upload_data($data));
    }

    public function del() {

        $_GPC = input();
        $id = $_GPC['id'];
        if ($this->request->isPost()) {
            $result = Db::name('member')->whereIn('id', $id)->update(['isagent' => 0]);
            if (!empty($result) || $result === 0) {
                json('success', 0, '删除成功！');
            } else {
                json('error', 1, '删除失败！');
            }
        }
        json('', 1, 'error');
    }

}
